<?php
/**
 * Class Profit
 * Desc  收益Model
 * Create on 2021/8/26 8:55
 * Create by wangyafang
 */

namespace app\api\model\xyb;


use think\Request;

class Profit extends Base
{
    protected $name = 'xyb_profit';

    protected $type = [
        'createtime'  =>  'timestamp:Y-m-d',
    ];

    public function getUsersList($user_id,$agent_id,$field = '*',$order = 'id desc'){
        $request = Request::instance();
        $param = $request->param();
        $page = isset($param['page']) ? $param['page'] : '';
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $model = new Profit();

        $count = $model->where(function ($query) use ($user_id, $agent_id) {
            $query->where('receive_id', $user_id)->whereOr('agent_id', $agent_id);
        })->count();
        if($page){
            $data = $model->where(function ($query) use ($user_id, $agent_id) {
                $query->where('receive_id', $user_id)->whereOr('agent_id', $agent_id);
            })->page($page,$limit)->order($order)->field($field)->select();
        }else{
            $data = $model->where(function ($query) use ($user_id, $agent_id) {
                $query->where('receive_id', $user_id)->whereOr('agent_id', $agent_id);
            })->order($order)->field($field)->select();
        }
        return ['total'=>$count, 'data'=>$data];
    }

    public static function saveProfit($orders, $distribution_rate){
        if(!$orders || !$distribution_rate){
            return false;
        }
       $forms = [
           'order_id'=>$orders['id'],
           'total_fee'=>$orders['total_fee'],
           'platform_id'=>1,
           'agent_id'=>$orders['agent_id'],
           'receive_id'=>$orders['receive_id'],
           'platfrom_profit'=>$distribution_rate['platform_amount'],
           'agent_profit'=>$distribution_rate['agent_amount'],
           'receive_profit'=>$distribution_rate['receive_amount'],
           'desc'=>'订单收益',
           'createtime'=>time()
       ];
        return self::insertGetId($forms);
    }
}